package com.cheng.example.book.advise151.chap1.client11;

import java.io.*;

/**
 *
 */
public class SerializationUtils {

    private static String FILE_NAME = "D:/obj.bin";

    //序列化
    public static void writeObject(Serializable s) {
        try {
            if (!new File(FILE_NAME).exists()) {
                new File(FILE_NAME).createNewFile();
            }
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
            oos.writeObject(s);
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //反序列化
    public static Object readObject() {
        Object obj = null;
        try {
            ObjectInputStream input = new ObjectInputStream(new FileInputStream(FILE_NAME));
            obj = input.readObject();
            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }
}